(function (win) {
  axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
  // 创建axios实例
  const service = axios.create({
    // axios中请求配置有baseURL选项，表示请求URL公共部分
    baseURL: '/',
    // 超时
    timeout: 1000000
  })
  function getToken(){
    return sessionStorage.getItem("token")
  }
  // request拦截器
  service.interceptors.request.use(
      config => {
        if(getToken()) config.headers['authorization'] = getToken()
        return config
      },
      error => {
        console.log(error)
        return Promise.reject(error)
      }
  )

  // 响应拦截器
  service.interceptors.response.use(res => {
      if (res.status===401) {// 返回登录页面
        window.location.href='/page/login/login.html'
        window.top.location.href = '/page/login/login.html'
      } else {
        return res.data
      }
    },
    error => {
      console.log('err' + error)
      let { message } = error;
      if (message === "Network Error") {
        message = "后端接口连接异常";
      }
      else if (message.includes("timeout")) {
        message = "系统接口请求超时";
      }
      else if (message.includes("Request failed with status code")) {
        message = "系统接口" + message.substr(message.length - 3) + "异常";
      }
      window.ELEMENT.Message({
        message: message,
        type: 'error',
        duration: 5 * 1000
      })
      return Promise.reject(error)
    }
  )
 win.$axios = service
})(window);
